package com.databinding.jetpacktesting;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.SavedStateHandle;

/**
 * Created by harisucici on 2020/3/11
 * Class description:
 */
public class MyViewModel extends AndroidViewModel {
    Context context;
    SavedStateHandle handle;

    MutableLiveData<Integer> aTeamScore;
    MutableLiveData<Integer> bTeamScore;
    MutableLiveData<String> wordDaoText;

    int count = 0;


    private int aBack, bBack;

    String key = "MY_KET";
    String spName = "SP_NAME";

//    public MyViewModel(@NonNull Application application) {
//        super(application);
//        context = application.getApplicationContext();
//    }

    public MyViewModel(@NonNull Application application, SavedStateHandle handle) {
        super(application);
        this.handle = handle;
        if (!handle.contains(key)) {
            load();
        }
        context = application.getApplicationContext();
    }

    public LiveData<Integer> getNumber() {
        return handle.getLiveData(key);
    }

    public MutableLiveData<String> getWordDaoText() {

        if (wordDaoText == null) {
            wordDaoText = new MutableLiveData<>();
            wordDaoText.setValue("开始:");
        }
        return wordDaoText;
    }

    public void setWordDaoText(String s) {
        this.wordDaoText.setValue(s);
    }

    public void load() {
        SharedPreferences sp = getApplication().getSharedPreferences(spName, Context.MODE_PRIVATE);
        int m = sp.getInt(key, 0);
        handle.set(key, m);
    }

    public void save() {
        SharedPreferences sp = getApplication().getSharedPreferences(spName, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putInt(key, getNumber().getValue());
        editor.apply();
    }

    public void add(int m) {
        handle.set(key, getNumber().getValue() + m);
    }

    public MutableLiveData<Integer> getaTeamScore() {
        if (aTeamScore == null) {
            aTeamScore = new MutableLiveData<>();
            aTeamScore.setValue(0);
        }
        return aTeamScore;
    }

    public MutableLiveData<Integer> getbTeamScore() {
        if (bTeamScore == null) {
            bTeamScore = new MutableLiveData<>();
            bTeamScore.setValue(0);
        }
        return bTeamScore;
    }

    public void addA(int m) {
        aBack = aTeamScore.getValue();
        bBack = bTeamScore.getValue();

        aTeamScore.setValue(aTeamScore.getValue() + m);
    }

    public void addB(int m) {

        aBack = aTeamScore.getValue();
        bBack = bTeamScore.getValue();
        bTeamScore.setValue(bTeamScore.getValue() + m);
    }

    public void reset() {
        aBack = aTeamScore.getValue();
        bBack = bTeamScore.getValue();
        aTeamScore.setValue(0);
        bTeamScore.setValue(0);
    }

    public void unDo() {
        aTeamScore.setValue(aBack);
        bTeamScore.setValue(bBack);
    }


}